((expr)=>{
    expr.local={
        set(key,val){
            let obj={};
            obj[key]=val;
            return new Promise((res,rej)=>{
                chrome.storage.local.set(obj, res);
            })
        },
        get(key){
            return new Promise((res,rej)=>{
                chrome.storage.local.get(key, (obj)=>{
                    res(obj[key]);
                });
            })
        },
        remove(key){
            return new Promise((res,rej)=>{
                chrome.storage.local.remove(key, res);
            })
        },
        clear(){
            return new Promise((res,rej)=>{
                chrome.storage.local.clear(res);
            })
        }
    };
    expr.config={
        config:null,
        set(conf){
            if(conf){
                this.config=conf;
            }
            local.set('SysConfig',this.config);
        },
        get(ok){
            if(!this.config||ok===true){
                //配置中读取
                let _self=this;
                return local.get('SysConfig').then((conf)=>{
                    if(conf){
                        _self.config=conf;
                    }
                    return conf;
                });
            }else{
                this.set();
                return this.config;
            }
        },
        update(conf,callback){
            let xf=conf,
                _self=this;
            function callFun(){
                let timer=setInterval(()=>{
                    if(conf.models.length<=conf.models.filter(x=>x.text||x.cache===false||!isNaN(x.cache*1)).length){
                        clearTimeout(out);
                        clearInterval(timer);
                        callback && callback();
                    }
                },10);
                let out=setTimeout(()=>{
                    clearInterval(timer);
                    callback && callback(true);
                },1000*60*3);
            }
            this.get(true).then(yf=>{
                if(!yf || yf.version!==conf.version){
                    //直接加载新文件
                    console.log('加载新文件');
                    conf.models.forEach((e)=>{
                        GetText(e.url,(txt)=>{
                            e.text=txt;
                            e.time=new Date().getTime();
                        })
                    });
                }else{
                    //匹配模块进行更新
                    console.log('更新文件');
                    conf.models.forEach((e)=>{
                        //判断版本是否一致
                        let y=yf.models.filter(x=>x.id===e.id)[0];
                        if(y){
                            if(e.ver!==y.ver){
                                //更新文件
                                GetText(e.url,(txt)=>{
                                    e.text=txt;
                                    e.time=new Date().getTime();
                                })
                            }else if(e.cache===undefined || e.cache===true){
                                //直接拷贝文件到最新文件中
                                e.text=y.text;
                                e.time=y.time||new Date().getTime();
                            }
                        }else if(e.cache===undefined || e.cache===true){
                            //直接更新
                            GetText(e.url,(txt)=>{
                                e.text=txt;
                                e.time=new Date().getTime();
                            })
                        }
                    })
                }
                callFun();
                //保存配置信息
                _self.set(conf);
            });
        },
        run(key,sync=false){
            function loadBackground(conf){
                if(!conf[key])return;
                conf[key].forEach((id)=>{
                    //获取配置
                    let list=conf.models.filter(x=>x.id===id);
                    if(list.length>0){
                        //运行
                        list.forEach((x)=>{
                            if(x.cache===false){
                                //远程加载
                                GetText(x.url,(txt)=>{
                                    eval(txt);
                                },sync);
                            }else if((!isNaN(x.cache) && (x.time+x.cache*1000)<new Date().getTime())){
                                //远程加载缓存
                                GetText(x.url,(txt)=>{
                                    eval(txt);
                                    x.text=txt;
                                    x.time=new Date();
                                },sync);
                            }else if(x.text){
                                eval(x.text);
                            }
                        });
                        //保存文件
                        config.set();
                    }
                });
            }
            let conf=this.get();
            if(conf.then){
                conf.then((conf)=>{
                    loadBackground(conf);
                });
            }else{
                loadBackground(conf);
            }
        }
    };
    expr.GetText=function(url,callback,sync=true) {
        let xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() {
            if(xmlHttp.readyState === 4){
                if(xmlHttp.status === 200){
                    callback && callback.call(xmlHttp,xmlHttp.responseText);
                }else{
                    callback && callback.call(xmlHttp,false);
                }
            }
        };
        xmlHttp.open("GET", url,
            sync);
        xmlHttp.setRequestHeader("If-Modified-Since","0");
        xmlHttp.setRequestHeader("Cache-Control","no-cache");
        xmlHttp.send();
    };
})(window);
